﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using AppfabricAdministrator.Commands;

namespace AdminMainUI
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class ConnectWindow : Window
    {
        private PowershellCommands psc=null;
        public ConnectWindow()
        {
            InitializeComponent();
            this.WindowStartupLocation = System.Windows.WindowStartupLocation.CenterOwner;
        }

        public PowershellCommands OpenConfigDialog()
        {
            this.ShowDialog();
            return psc;
        }

        private void btnBrowse_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog dialog=new FolderBrowserDialog();
            txtSharedLocation.Text = GetNetworkFolder(dialog);
        }

        private String GetNetworkFolder(FolderBrowserDialog dialog)
        {
            dialog.SelectedPath = txtSharedLocation.Text;
            if(dialog.ShowDialog()==System.Windows.Forms.DialogResult.OK)
                return dialog.SelectedPath;
            else
                return "";
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (String.IsNullOrEmpty(txtSharedLocation.Text))
                {
                    System.Windows.MessageBox.Show(grpProvider.Header+" cannot be empty!", "Mandatory Field", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
                else
                {
                    psc = new PowershellCommands(cmbProviderType.SelectionBoxItem.ToString(), txtSharedLocation.Text);
                    psc.GetCacheHosts();
                    psc.GetCache();
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message, "Error");
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void cmbProviderType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!String.IsNullOrEmpty(cmbProviderType.Text))
            {
                if (((ComboBoxItem)(cmbProviderType.SelectedItem)).Content.ToString() == "XML")
                {
                    grpProvider.Header = "XML Location";
                    btnBrowse.Visibility = System.Windows.Visibility.Visible;
                    txtSharedLocation.Width = 278;
                }
                else
                {
                    grpProvider.Header = "SQL Connection String";
                    btnBrowse.Visibility = System.Windows.Visibility.Hidden;
                    txtSharedLocation.Width = 361;
                }
            }
        }
    }
}
